package com.jvyou.singleton.lazy;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/7/29 16:17
 * @Description 对象锁实现单例模式, 可以在`getInstance`方法上面加一个锁保证线程安全，但是效率会降低
 */
public class LockSingleton {
    private static LockSingleton instance;

    private LockSingleton() {
    }

    public static synchronized LockSingleton getInstance() {
        if (instance == null) {
            instance = new LockSingleton();
        }
        return instance;
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(LockSingleton.getInstance().hashCode())).start();
        }
    }
}
